<template>
  <div>
    <div class="btnStyle">
      <el-button type="primary" :loading="submit_loading" @click="submit()" class="saveBtn" size="small" v-if="isShowBtn">保存当前信息</el-button>
    </div>
    <div class="content">
      <el-form ref="fundFormRef" :model="fundForm" class="form_box">
        <el-row class="lable_box">
          <el-col :span="8" class="lable">科目名称</el-col>
          <el-col :span="8" class="lable">金额（万元）</el-col>
          <el-col :span="8" class="lable">备注</el-col>
        </el-row>
        <el-row class="row_box">
          <el-col :span="8" class="row_lable">
            <el-tree default-expand-all :data="lableData" :props="defaultProps" @node-click="handleNodeClick" />
          </el-col>
          <el-col :span="8" class="row_con">
            <el-form-item></el-form-item>
            <el-form-item :error="errorList[item.key]" v-for="(item,index) in lableData[0].children" :key="index">
              <el-input v-model="fundForm[item.key]" maxlength="13" show-word-limit placeholder="请输入金额" @blur="validitem" :disabled="!isShowBtn" />
            </el-form-item>
            <el-form-item :error="errorList['expectedRevenue']">
              <el-input v-model="fundForm.expectedRevenue" maxlength="13" show-word-limit placeholder="请输入金额"
                @blur="validitem2($event, 'expectedRevenue')" :disabled="!isShowBtn" />
            </el-form-item>
            <el-form-item :error="errorList['unexpectedRevenue']">
              <el-input v-model="fundForm.unexpectedRevenue" maxlength="13" show-word-limit placeholder="请输入金额"
                @blur="validitem2($event, 'unexpectedRevenue')" :disabled="!isShowBtn" />
            </el-form-item>
            <el-form-item :error="errorList['totalCost']">
              <el-input v-model="fundForm.totalCost" maxlength="13" show-word-limit placeholder="请输入金额" @blur="validitem2($event, 'totalCost')"
                :disabled="!isShowBtn" />
            </el-form-item>
          </el-col>
          <el-col :span="8" class="row_con">
            <el-form-item></el-form-item>
            <el-form-item v-for="(item,index) in lableData[0].children" :key="index">
              <div v-if="!isShowBtn">
                <el-tooltip class="item" effect="dark" :content="fundForm[item.commentKey] || '暂未填写'" placement="top-start">
                  <el-input v-model="fundForm[item.commentKey]" maxlength="64" placeholder="请输入备注" :disabled="!isShowBtn" />
                </el-tooltip>
              </div>
              <el-input v-else v-model="fundForm[item.commentKey]" maxlength="64" placeholder="请输入备注" :disabled="!isShowBtn" />
            </el-form-item>
            <el-form-item>
              <div v-if="!isShowBtn">
                <el-tooltip class="item" effect="dark" :content="fundForm.comment9 || '暂未填写'" placement="top-start">
                <el-input v-model="fundForm.comment9" maxlength="64" placeholder="请输入备注" :disabled="!isShowBtn" />
                </el-tooltip>
              </div>
              <el-input v-else v-model="fundForm.comment9" maxlength="64" placeholder="请输入备注" :disabled="!isShowBtn" />
            </el-form-item>
            <el-form-item>
              <div v-if="!isShowBtn">
                <el-tooltip class="item" effect="dark" :content="fundForm.comment10 || '暂未填写'" placement="top-start">
                <el-input v-model="fundForm.comment10" maxlength="64" placeholder="请输入备注" :disabled="!isShowBtn" />
                </el-tooltip>
              </div>
              <el-input v-else v-model="fundForm.comment10" maxlength="64" placeholder="请输入备注" :disabled="!isShowBtn" />
            </el-form-item>
            <el-form-item>
              <div v-if="!isShowBtn">
                <el-tooltip class="item" effect="dark" :content="fundForm.comment11 || '暂未填写'" placement="top-start">
                <el-input v-model="fundForm.comment11" maxlength="64" placeholder="请输入备注" :disabled="!isShowBtn" />
                </el-tooltip>
              </div>
              <el-input v-else v-model="fundForm.comment11" maxlength="64" placeholder="请输入备注" :disabled="!isShowBtn" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
  </div>
</template>

<script>
import { saveOrUpdateFundEighteen } from '@/api/indicatorInput'
export default {
  components: {},
  props: {
    isShowBtn: {
      type: Boolean,
      default: false
    },
    projectId: {
      type: String,
      default: ''
    },
    projectStage: {
      type: String,
      default: ''
    },
    fundEighteen: {
      type: Object,
      default: () => { }
    }
  },
  data() {
    return {
      subtotal: 0,
      subtotalMsg: '',
      isShow: true,
      submit_loading: false,
      errorList: {
        amount: '',
        budgetEstimate: '',
        equipmentCost: '',
        materialsBudget: '',
        outsourceBudget: '',
        ignitionBudget: '',
        exchangeBudget: '',
        businessBudget: '',
        labourBudget: '',
        consultBudget: '',
        otherBudget: '',
        externalCost: '',
        salary: ''
      },
      // fundForm2: {
      //   comment1: '',
      //   comment2: '',
      //   comment3: '',
      //   comment4: '',
      //   comment5: '',
      //   comment6: '',
      //   comment7: '',
      //   comment8: '',
      //   comment9: '',
      //   comment10: '',
      // },
      projectFundId: '',
      fundForm: {
        fundId: '',
        materialCost: 0,
        dedicatedCost: 0,
        fuelCost: 0,
        publishCost: 0,
        fixedCost: 0,
        manageCost: 0,
        test: 0,
        expectedRevenue: 0,
        unexpectedRevenue: 0,
        totalCost: 0,
        externalCost: 0,
        salary: 0,
        comment1: '',
        comment2: '',
        comment3: '',
        comment4: '',
        comment5: '',
        comment6: '',
        comment7: '',
        comment8: '',
        comment9: '',
        comment10: '',
        comment11: '',
      },
      lableData: [
        {
          id: 1,
          label: '（一）预计成本',
          children: [
            {
              id: 4,
              label: '1.材料费',
              key: 'materialCost',
              commentKey: 'comment1'
            },
            {
              id: 5,
              label: '2.专用费',
              key: 'dedicatedCost',
              commentKey: 'comment2'
            },
            {
              id: 6,
              label: '3.外协费',
              key: 'externalCost',
              commentKey: 'comment3'
            },
            {
              id: 7,
              label: '4.燃料动力费',
              key: 'fuelCost',
              commentKey: 'comment4'
            },
            {
              id: 8,
              label: '5.事务费',
              key: 'publishCost',
              commentKey: 'comment5'
            },
            {
              id: 9,
              label: '6.固定资产折旧费',
              key: 'fixedCost',
              commentKey: 'comment6'
            },
            {
              id: 10,
              label: '7.管理费',
              key: 'manageCost',
              commentKey: 'comment7'
            },
            {
              id: 11,
              label: '8.工资及劳务费',
              key: 'salary',
              commentKey: 'comment8'
            },
          ]
        },
        {
          id: 2,
          label: '（二）预计收益'
        },
        {
          id: 3,
          label: '（二）不可预见费'
        },
        {
          id: 3,
          label: '合计'
        }
      ],
      defaultProps: {
        children: 'children',
        label: 'label'
      }
    }
  },
  created() {
    Object.assign(this.fundForm, this.fundEighteen)
    // Object.assign(this.fundForm, {
    //   fundId: this.fundEighteen&&this.fundEighteen.fundId,
    //   materialCost: this.fundEighteen&&this.fundEighteen.materialCost,
    //   dedicatedCost: this.fundEighteen&&this.fundEighteen.dedicatedCost,
    //   fuelCost: this.fundEighteen&&this.fundEighteen.fuelCost,
    //   publishCost: this.fundEighteen&&this.fundEighteen.publishCost,
    //   fixedCost: this.fundEighteen&&this.fundEighteen.fixedCost,
    //   manageCost: this.fundEighteen&&this.fundEighteen.manageCost,
    //   expectedRevenue: this.fundEighteen&&this.fundEighteen.expectedRevenue,
    //   unexpectedRevenue: this.fundEighteen&&this.fundEighteen.unexpectedRevenue,
    //   totalCost: this.fundEighteen&&this.fundEighteen.totalCost,
    //   externalCost: this.fundEighteen&&this.fundEighteen.externalCost,
    // }
    // )
    // Object.assign(this.fundForm2,
    //   {
    //     comment1: this.fundEighteen&&this.fundEighteen.comment1,
    //     comment2: this.fundEighteen&&this.fundEighteen.comment2,
    //     comment3: this.fundEighteen&&this.fundEighteen.comment3,
    //     comment4: this.fundEighteen&&this.fundEighteen.comment4,
    //     comment5: this.fundEighteen&&this.fundEighteen.comment5,
    //     comment6: this.fundEighteen&&this.fundEighteen.comment6,
    //     comment7: this.fundEighteen&&this.fundEighteen.comment7,
    //     comment8: this.fundEighteen&&this.fundEighteen.comment8,
    //     comment9: this.fundEighteen&&this.fundEighteen.comment9,
    //     comment10: this.fundEighteen&&this.fundEighteen.comment10,
    //   }

    // )
  },

  methods: {
    validitem() {
      let total = 0
      let valid = true
      const { ...computeList } = this.fundForm

      for (const i in computeList) {
        const res = this.moneyInput(computeList[i], i)
        this.errorList[i] = res.msg
        if (res.state) {
          total = parseFloat(computeList[i]) + parseFloat(total)
        } else {
          valid = false
        }
      }
      if (valid) {
        this.subtotalMsg = ''
        this.subtotal = total.toFixed(2)
      } else {
        this.subtotalMsg = '计算出错，请重新填写'
      }
    },
    validitem2(e, item) {
      const res = this.moneyInput(e.target.value, item)
      this.errorList[item] = res.msg
    },
    goBack() {
      this.$router.go(-1)
    },
    handleNodeClick(data) {
      console.log(data)
      if (data.id == 2) {
        this.isShow = !this.isShow
      }
    },
    valid(callback) {
      let valid = true
      for (const i in this.fundForm) {
        const res = this.moneyInput(this.fundForm[i], i)
        this.errorList[i] = res.msg
        if (!res.state) {
          valid = false
        }
      }
      if (valid) {
        if (
          parseFloat(this.fundForm.budgetEstimate) >=
          parseFloat(this.fundForm.amount) + parseFloat(this.subtotal)
        ) {
          callback(true)
        } else {
          this.$message.error('校验失败,费用额度超出预算')
          callback(false)
        }
      }
    },
    submit() {
      const params = {
        ...this.fundForm,
        // ...this.fundForm2,
        projectStage: this.projectStage,
        projectId: this.projectId
      }
      saveOrUpdateFundEighteen(params)
        .then(() => {
          this.$emit('initData', this.projectStage)
        })
    }
  }
}
</script>

<style lang="less" scoped>
  .header {
    width: 100%;
    height: 50px;
    margin-top: 2px;
    padding-left: 25px;
    background-color: #fff;
    display: flex;
    align-items: center;
    font-size: 18px;
    font-family: 'PingFangSC-Regular', 'PingFang SC', sans-serif;
    font-weight: 400;
    font-size: 18px;
    color: #000000;

    .icon-back {
      margin-right: 10px;
      cursor: pointer;
    }
  }

  .content {
    font-size: 16px;
    background-color: #fff;

    .top_btn {
      text-align: right;
      margin: 20px 0;
    }

    .form_box {
      .lable_box {
        background: #f3f4f6;
        font-size: 16px;
        font-family: 'PingFangSC-Regular', 'PingFang SC', sans-serif;
        font-weight: 700;
        font-size: 18px;
        color: #000000;
        height: 50px;
        display: flex;
        align-items: center;

        .lable {
          text-align: center;
        }
      }

      .row_box {
        border: 1px solid #ebeef5;
        height: 625px;

        .row_lable {
          padding: 20px 0 20px 30px;
          height: 100%;
        }

        .row_con {
          height: 100%;
          border-left: 1px solid #ebeef5;
          padding: 20px 0 20px 0;
        }
      }
    }

    .bottom_btn {
      text-align: center;
      margin: 50px 0 20px;
    }

    ::v-deep .el-form-item {
      margin: 0;
      display: flex;
      align-items: center;
      justify-content: center;
      padding: 0 50px;
      height: 45px;
    }

    ::v-deep .el-form-item__content {
      width: 100%;
    }

    ::v-deep .el-tree-node__content {
      height: 45px;
    }

    ::v-deep .el-input__inner {
      height: 25px;
      line-height: 25px;
    }

    ::v-deep .el-form-item__error {
      color: #f56c6c;
      font-size: 12px;
      line-height: 1;
      padding-top: 0;
      position: absolute;
      top: 90%;
      left: 0;
    }
  }
  .btnStyle {
    position: relative;
    .saveBtn {
      position: absolute;
      right: 0px;
      top: -50px;
    }
  }
  :deep(.el-input.is-disabled .el-input__inner) {
    background: #fff !important;
    color: #333 !important;
    font-size: 18px !important;
  }
  :deep(.el-input .el-input__inner) {
    font-size: 18px !important;
  }
</style>
